{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'F:/demo.jpg'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-f34ddb600706>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mhttp\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0murllib3\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPoolManager\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'https://aip.baidubce.com/rest/2.0/ocr//general?access token='\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0maccess_token\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'F:/demo.jpg'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'rb'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[1;31m#参数image:图像base64编码\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mimg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbase64\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mb64encode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'F:/demo.jpg'"
     ]
    }
   ],
   "source": [
    "import urllib3, base64\n",
    "from urllib.parse import urlencode\n",
    "access_token='自己应用信息获取的 access_token'\n",
    "http=urllib3.PoolManager()\n",
    "url='https://aip.baidubce.com/rest/2.0/ocr//general?access token='+access_token\n",
    "f = open('F:/demo.jpg','rb')\n",
    "#参数image:图像base64编码\n",
    "img = base64.b64encode(f.read())\n",
    "params={'image':img}\n",
    "#对base64数据urlencode进行处理\n",
    "params=urlencode(params)\n",
    "request=http.request('POST',\n",
    "                      url,\n",
    "                      body=params,\n",
    "                      headers={'Content-Type':'application/x-www-form-urlencoded'})\n",
    "#对返回的byte字节进行处理。 Python33输出位串,而不是可读的字符串,需要进行转换\n",
    "result = str(request.data,'utf-8')\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "{\n",
    "    \"log_id\": ,\n",
    "    \"words_result_num\": 1,\n",
    "    \"words_result\": [\n",
    "        {\n",
    "            \"location\": {\n",
    "                \"width\": 145\n",
    "                \"top\": 8\n",
    "                \"height\": 18\n",
    "                \"left\": 10\n",
    "            },\n",
    "            \"words\": \"通用文字识别代码\"\n",
    "        }\n",
    "    ]\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
